#sys.path.append("..")
#from memory import memory
import RPi.GPIO as GPIO
import time
from adafruit_servokit import ServoKit
class action:
    def __init__(self):
        
        #set servo 16 channnels controller
        self.kit = ServoKit(channels=16)
        #set GPIO 17 for detect the audio reciver
        channel = 17
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(channel,GPIO.IN)
        GPIO.add_event_detect(channel,GPIO.BOTH,bouncetime=300)
        #GPIO.add_event_callback(channel,callback)

        #def callback(channel):
        self.good = GPIO.input(channel)
    
    def actPick(self):
        self.kit.servo[8].angle = 50
        time.sleep(1)
        self.kit.servo[8].angle = 80
        time.sleep(1)
        self.kit.servo[0].angle = 30
        time.sleep(1)
        self.kit.servo[0].angle = 70
        time.sleep(1)
        self.kit.servo[0].angle = 0
    
    def tryOut(self):     
        #interact with people
        print("please choose modle between tryout or use and type it")
        modle = input("enter tryout/use:")
        if modle == "tryout":
            info = True
        if modle == "use":
            info = False
        #interact with people and choose try or use
        if info:
            #try thing you want
            a = 6 
            while good==1 :
                #good = GPIO.input(channel)
                kit.servo[8].angle = 10 + a
                time.sleep(1)
                kit.servo[0].angle = 30
                time.sleep(1)
                kit.servo[0].angle = 70
                time.sleep(1)
                kit.servo[0].angle = 0
                a += 15
                print("please tell me is that ok?")
                tellit = input("enter y/n:")
                if tellit == "y":
                    good = 0
                if tellit == "n":
                    good = 1
                #time.sleep(0.5)
                #good = GPIO.input(channel)
            else:
                TM = TryStore()
                number = 10 + a
                codeT = "kit.servo[8].angle = {}".format(number)
                TM.update_code(codeT)
                kit.servo[8].angle = 10
                print("it is tryed end and stored")

        else:
            TS = TryStore()
            position = "first"
            codeT = TS.use_code(position)
            #print(codeT)
            #typ = type(codeT)
            #print(typ)
            stringone = str(codeT)
            stringtwo = stringone.replace('(','')
            stringthree = stringtwo.replace(')','')
            stringfour = stringthree.replace(',','')
            stringfive = stringfour.replace("'","")
            #print(stringfour)
            #stringcode = 'kit.servo[8].angle=20'
            exec(stringfive)
            #kit.servo[8].angle = 10
            print(stringfive)
            time.sleep(1)
            kit.servo[0].angle = 30
            time.sleep(1)
            kit.servo[0].angle = 70
            time.sleep(1)
            kit.servo[8].angle = 1
            time.sleep(2)
            kit.servo[0].angle = 0
